<?php
/* 
 * This class classifies the data according to a specific category given the set of words by using SVM.
 * Uses the SVM PHP implementation written by Ian Barber, with some modifications to suit the requirements of this class.
 * Outputs the probability of said data belonging to that category.
 * 
 * Author: Baron Gerarld S. Manzano
 *
 */

include "phpsvm_lib/svm.php";

class SQClassifier {
	protected $words = array();
	protected $classification = '';
	protected $initialdata = array();
	protected $initialvalues = array();
	protected $classvalues = array();
	protected $svm = NULL;
	protected $ticks = 0;
	
	/*
	 * Instantiates the SQClassifier class given the array of words from the SQCrawler class.
	 * This class won't work without running the svminit.php file at the server first.
	 */
	public function __construct($words) {
		set_time_limit(30);
		
		$this->words = $words;
		
		$this->initializeType("training");
		
		$this->initializeWords();
		if($this->ticks > 0){
			$this->svm = new PHPSVM();
			$this->classify();
			unset($this->svm);
		}
	}
	
	/*
	 * Gets the corresponding values for the dictionary of words given the classfile.txt and the classfile_values.dat.
	 * Sets the words and values in an array.
	 */
	public function initializeType($classfile){
		$file = $classfile . ".txt";
		$data = file_get_contents($file);
		$token = strtok($data,"\n\r");
		
		while($token != false){
			$this->initialdata[] = $token;
			$token = strtok("\n\r");
		}
		
		$file = $classfile . "_values.dat";
		$data = file_get_contents($file);
		$token = strtok($data,"\n\r");
		
		while($token != false){
			$this->initialvalues[] = $token;
			$token = strtok("\n\r");
		}
	}
	
	/*
	 * Initializes the values of the words gathered at the webpage.
	 * If the word is found at the webpage, set the default value given by the classfile_values.dat
	 * Otherwise, sets the value to 0.
	 */
	public function initializeWords(){
		$fname = "input.dat";
		$fh = fopen($fname, 'w') or die("error");
		$size = count($this->words);
		
		for($c=0;$c<$size;$c++){
			$input = "";
			if(in_array($this->words[$c], $this->initialdata)){
				$index = array_search($this->words[$c], $this->initialdata);
				$input = $this->initialvalues[$index];
				$this->ticks++;
			}
			else{
				$input = "0 ";
				$temp = $c + 1;
				$input = $input . $temp . ":0\n";
			}
			fwrite($fh, $input);
		}
		fclose($fh);
	}
	
	/*
	 * Calls the test() function from the PHPSVM library.
	 * Gives a classification based on the result.
	 */
	public function classify(){
		$this->classvalues[] = (float) $this->svm->test('input.dat', 'training.svm', 'output.dat');

		if($this->classvalues[0] < 0) $this->classification = 'leisure';
		else if($this->classvalues[0] > 0) $this->classification = 'academics';
		return;
	}
	
	/*
	 * Getter function for the classification.
	 */
	public function getClassification(){
		if($this->classification == '')
			return 'unclassifiable';
		return $this->classification;
	}
	
	/*
	 * Refreshes the values of this specific instance.
	 * Used for debugging purposes.
	 */
	public function refresh(){
		$this->words = array();
		$this->classification = '';
	}
	
}

?>